package com.phx.serialjni;//package com.test;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.BaseColumns;



import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import static com.phx.serialjni.MainActivityf.db;


/**
 * 实现对表的创建、更新、变更列名操作
 * @author ytm0220@163.com
 *
 */
public class SQLiteHelper extends SQLiteOpenHelper {
    public static final String TB_NAME_IMAGE= "TBNameimage";
    public static final String TB_NAME_BIN13 = "TBNamebin13";
    //数据库的字段
    public static class PictureColumns implements BaseColumns {
        public static final String PICTURE = "picture";
        public static final String NAME = "name";
        public static final String BIN = "BIN";

    }
    public SQLiteHelper(Context context, String name, CursorFactory factory,
                        int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }


    /**
     * 创建新表
     */
    //@Override
    public void DBCreate(SQLiteDatabase db,String TB_NAME) {
        String sql =null;
        if(TB_NAME=="TB_NAME_IMAGE") {
            sql = "create table if not exists " + TB_NAME_IMAGE + "(" + BaseColumns._ID
                    + " integer primary key autoincrement,"+ PictureColumns.PICTURE
                      + " blob not null ," +  PictureColumns.NAME + " text);";
            /*  sql = "create table if not exists " + TB_NAME_IMAGE + "(" + BaseColumns._ID
                    + " integer primary key autoincrement,"+ PictureColumns.PICTURE
                     + " blob not null);";*/
        }
        if(TB_NAME=="TB_NAME_BIN13") {
            sql = "create table if not exists " + TB_NAME_BIN13 + "(" + BaseColumns._ID
                    + " integer primary key autoincrement," + PictureColumns.BIN
                    + " blob not null);";
        }
        db.execSQL(sql);

       // String  sql = "create table if not exists " + TB_NAME + " (studentid text primary key, name text, sex text, age integer)";
       // db.execSQL(sql);

       // db.execSQL("create table if not exists " +
         //       TB_NAME+ " (studentid text primary key, name text, sex text, age integer)");
    }
    public void PutBitbmpToDataBase(Bitmap bm,String imageName) {
        BitmapDrawable drawable =  new  BitmapDrawable(bm);
        drawable = new BitmapDrawable( bm);
        // Drawable drawable = context.getResources().getDrawable(R.drawable.a1);
        ContentValues cv = new ContentValues();
        cv.put(PictureColumns.PICTURE, getPicture(drawable));
          // db.insert(TB_NAME_IMAGE, imageName, cv);
        cv.put(PictureColumns.NAME, imageName);
            db.insert(TB_NAME_IMAGE, null, cv);

    }
    public void Put13BinToDataBase (byte[] byt) {
        ContentValues cv = new ContentValues();
        cv.put(PictureColumns.BIN, byt);
        db.insert(TB_NAME_BIN13, null, cv);
    }

    //将drawable转换成可以用来存储的byte[]类型
    private byte[] getPicture(Drawable drawable) {
        if(drawable == null) {
            return null;
        }
        BitmapDrawable bd = (BitmapDrawable) drawable;
        Bitmap bitmap = bd.getBitmap();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
        return os.toByteArray();
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public void DBUpgrade(SQLiteDatabase db, int oldVersion, int newVersion,String TB_NAME) {

        if(TB_NAME=="TB_NAME_IMAGE") {
            db.execSQL("DROP TABLE IF EXISTS " + TB_NAME_IMAGE);
        }
        if(TB_NAME=="TB_NAME_BIN13") {
            db.execSQL("DROP TABLE IF EXISTS " + TB_NAME_BIN13);
        }
        DBCreate(db,TB_NAME);
    }

    /**
     * 变更列名
     * @param db
     * @param oldColumn
     * @param newColumn
     * @param typeColumn
     */
    public void updateColumn(SQLiteDatabase db, String oldColumn, String newColumn, String typeColumn,String TB_NAME) {

        try{
            if(TB_NAME=="TB_NAME_IMAGE") {
                db.execSQL("ALTER TABLE " +
                        TB_NAME_IMAGE + " CHANGE " +
                        oldColumn + " "+ newColumn +
                        " " + typeColumn
                );
            }
            if(TB_NAME=="TB_NAME_BIN13") {
                db.execSQL("ALTER TABLE " +
                        TB_NAME_BIN13 + " CHANGE " +
                        oldColumn + " "+ newColumn +
                        " " + typeColumn
                );
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
